﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Input;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using FlatRedBall.Graphics.Animation;
namespace ROMAP.Entities.Menu
{
    public class MenuButton : PositionedObject
    {

        #region Fields

        private string filePath;
        Sprite _sprite;
        string contentManagerName;
        public AxisAlignedRectangle collision;
        Text buttonText;
        string buttonString;
        char type;

        #endregion

        #region Properties

        #endregion

        #region Methods

        //Constructor
        public MenuButton(string contentManageName, string filepath, string text, char typer, float xPos, float yPos)
        {
            contentManagerName = contentManageName;
            filePath = filepath;
            buttonString = text;
            type = typer;
            this.X = xPos;
            this.Y = yPos;
            this.Z = 2.0f;
            Initialize(true);
        }

        protected virtual void Initialize(bool addToManagers)
        {
            if (addToManagers)
            {
                AddToManagers(null);
            }
        }

        public virtual void AddToManagers(Layer layerToAddTo)
        {
            SpriteManager.AddPositionedObject(this);
            InitializeAnimations();
            //_sprite = SpriteManager.AddSprite(filePath, contentManagerName);
            _sprite.AttachTo(this, false);

            float pixelsPerUnit = SpriteManager.Camera.PixelsPerUnitAt(_sprite.Z);
            _sprite.ScaleX = .5f * _sprite.Texture.Width / pixelsPerUnit;
            _sprite.ScaleY = .5f * _sprite.Texture.Height / pixelsPerUnit;


            buttonText = TextManager.AddText(buttonString);
            buttonText.AttachTo(this, false);
            buttonText.RelativeZ = 1;
            buttonText.HorizontalAlignment = HorizontalAlignment.Center;
            buttonText.Scale = .5f;
            buttonText.Spacing = .65f;
            buttonText.ColorOperation = ColorOperation.Modulate;
            buttonText.Red = 0f;
            buttonText.Green = 0f;
            buttonText.Blue = 0f;

            this.collision = ShapeManager.AddAxisAlignedRectangle();
            this.collision.AttachTo(this, false);
            this.collision.Visible = false;
            this.collision.ScaleX = .5f * _sprite.Texture.Width / pixelsPerUnit;
            this.collision.ScaleY = .5f * _sprite.Texture.Height / pixelsPerUnit;
        }

        public virtual void Destroy()
        {
            SpriteManager.RemoveSprite(_sprite);
            SpriteManager.RemovePositionedObject(this);
            ShapeManager.Remove(collision);
            TextManager.RemoveText(buttonText);

        }

        private void InitializeAnimations()
        {
            // Loads the animation chain for when the buckle opens.
            AnimationChainList ani = new AnimationChainList();
            AnimationChain idle = new AnimationChain();
            AnimationChain hover = new AnimationChain();

            idle.Add(new AnimationFrame(filePath, 0.06f, "Global"));
            hover.Add(new AnimationFrame(filePath + "Hover", 0.06f, "Global"));

            idle.Name = "idle";
            hover.Name = "hover";

            ani.Add(idle);
            ani.Add(hover);


            _sprite = SpriteManager.AddSprite(ani);
            _sprite.CurrentChainName = "idle";
            _sprite.Animate = false;
            _sprite.CurrentFrameIndex = 0;
        }

        public void Idle()
        {
            _sprite.CurrentChainName = "idle";
        }

        public void Hover()
        {
            _sprite.CurrentChainName = "hover";
        }

        public void MultScaleX(float value)
        {
            _sprite.ScaleX *= value;
            this.collision.ScaleX *= value;
        }

        public void MultScaleY(float value)
        {
            _sprite.ScaleY *= value;
            this.collision.ScaleY *= value;
        }

        #endregion
    }
}
